package com.tudouji.project.copy.controller;

import java.util.List;

import com.tudouji.common.enums.CopyStatusEnums;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.project.copy.domain.Copytokd;
import com.tudouji.project.copy.service.ICopytokdService;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.web.page.TableDataInfo;

/**
 * 待同步数据Controller
 *
 * @author wf
 * @date 2021-01-07
 */
@RestController
@RequestMapping("/copy/copytokd")
public class CopytokdController extends BaseController {
    @Autowired
    private ICopytokdService copytokdService;

    /**
     * 查询待同步数据列表
     */
    @PreAuthorize("@ss.hasPermi('copy:copytokd:list')")
    @GetMapping("/list")
    public TableDataInfo list(Copytokd copytokd) {
        startPage();
        List<Copytokd> list = copytokdService.selectCopytokdList(copytokd);
        return getDataTable(list);
    }

    /**
     * 导出待同步数据列表
     */
    @PreAuthorize("@ss.hasPermi('copy:copytokd:export')")
    @Log(title = "待同步数据", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Copytokd copytokd) {
        List<Copytokd> list = copytokdService.selectCopytokdList(copytokd);
        ExcelUtil<Copytokd> util = new ExcelUtil<Copytokd>(Copytokd.class);
        return util.exportExcel(list, "copytokd");
    }

    /**
     * 获取待同步数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('copy:copytokd:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(copytokdService.selectCopytokdById(id));
    }

    /**
     * 新增待同步数据
     */
    @PreAuthorize("@ss.hasPermi('copy:copytokd:add')")
    @Log(title = "待同步数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Copytokd copytokd) {
        return toAjax(copytokdService.insertCopytokd(copytokd));
    }

    /**
     * 修改待同步数据
     */
    @Log(title = "待同步数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Copytokd copytokd) {
        return toAjax(copytokdService.updateCopytokd(copytokd));
    }

    /**
     * 删除待同步数据
     */
    @PreAuthorize("@ss.hasPermi('copy:copytokd:remove')")
    @Log(title = "待同步数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(copytokdService.deleteCopytokdByIds(ids));
    }


    /**
     * 重新同步单个数据
     *
     * @return
     */
    public AjaxResult repaetSync() {
        return AjaxResult.success("重新获取成功");
    }

    @Log(title = "待同步数据", businessType = BusinessType.UPDATE)
    @GetMapping("/update")
    public AjaxResult update(Long id) {
        logger.info("***********id:{}",id);
        Copytokd copytokd=new Copytokd();
        copytokd.setId(id);
        copytokd.setStatus(CopyStatusEnums.WAIT.getCode());
        copytokd.setCopyRemark(CopyStatusEnums.WAIT.getInfo());
        return toAjax(copytokdService.updateCopytokd(copytokd));
    }
}
